//---------------------------------------------------------------------
// File: EntLibDatabaseRowCountStep.cs
// 
// Summary: 
//---------------------------------------------------------------------


using System;
using System.Xml;
using BizUnit.BizUnitOM;
using BizUnit.Extensions.EntLib.Utilities;

namespace BizUnit.Extensions.EntLib
{
    /// <summary>
    /// The EntLibDatabaseRowCountStep step uses the supplied configuration file, database configuration name, table name and condition to determine the number of rows in the database
    /// and validates this against the expected rows.
    /// </summary>
    /// 
    /// <remarks>
    /// The following shows an example of the Xml representation of this test step.
    /// 
    /// <code escaped="true">
    ///	<TestStep assemblyPath="" typeName="BizUnit.EntLibDatabaseRowCountStep">
    ///		<ConfigFileLocation>..\\..\\db.config</ConfigFileLocation>
    ///		<DatabaseConfigName>Northwind</DatabaseConfigName>
    ///		<Table>table</Table>
    ///		<Condition>id=1</Condition>
    ///		<ExpectedRows>100</ExpectedRows>
    ///	</TestStep>
    ///	</code>
    ///	
    ///	<list type="table">
    ///		<listheader>
    ///			<term>Tag</term>
    ///			<description>Description</description>
    ///		</listheader>
    ///		<item>
    ///			<term>ConfigFileLocation</term>
    ///			<description>The config file containing the Enterprise Library configuration for db access</description>
    ///		</item>
    ///     <item>
    ///			<term>DatabaseConfigName</term>
    ///			<description>The config section name of the databased used in the test</description>
    ///		</item>
    ///		<item>
    ///			<term>Table</term>
    ///			<description>Table name select rows from</description>
    ///		</item>
    ///		<item>
    ///			<term>Condition</term>
    ///			<description>Condition to use</description>
    ///		</item>
    ///		<item>
    ///			<term>ExpectedRows</term>
    ///			<description>The number of expected rows</description>
    ///		</item>
    ///	</list>
    ///	</remarks>

    public class EntLibDatabaseRowCountStep : ITestStep, ITestStepOM
    {
        private string configFileLocation;
        private string databaseConfigName;
        private string table;
        private string condition;
        private int expectedRows;

        public string ConfigFileLocation
        {
            get
            {
                return configFileLocation;
            }
            set
            {
                configFileLocation = value;
            }
        }
        public string DatabaseConfigName
        {
            get
            {
                return databaseConfigName;
            }
            set
            {
                databaseConfigName = value;
            }
        }
        public string Table
        {
            get
            {
                return table;
            }
            set
            {
                table = value;
            }
        }
        public string Condition
        {
            get
            {
                return condition;
            }
            set
            {
                condition = value;
            }
        }

        public int ExpectedRows
        {
            get
            {
                return expectedRows;
            }
            set
            {
                expectedRows = value;
            }
        }
        /// <summary>
        /// ITestStep.Execute() implementation
        /// </summary>
        /// <param name='testConfig'>The Xml fragment containing the configuration for this test step</param>
        /// <param name='context'>The context for the test, this holds state that is passed beteen tests</param>
        public void Execute(XmlNode testConfig, Context context)
        {
            ConfigFileLocation = context.ReadConfigAsString(testConfig, "ConfigFileLocation");
            DatabaseConfigName = context.ReadConfigAsString(testConfig, "DatabaseConfigName");
            Table = context.ReadConfigAsString(testConfig, "Table");
            Condition = context.ReadConfigAsString(testConfig, "Condition");
            ExpectedRows = context.ReadConfigAsInt32(testConfig, "ExpectedRows");

            Execute(context);


        }

        #region ITestStepOM Members

        public void Execute(Context context)
        {
            Validate(context);
            // Build SQL statement
            string sqlStatement = "select count(*) from " + table + " where " + condition;

            context.LogInfo("EntLibDatabaseRowCountStep connecting to {0}, executing statement {1}", databaseConfigName, sqlStatement);

            // Execute command against specified database
            int rows = EntLibDatabaseHelper.ExecuteScalar(configFileLocation, databaseConfigName, sqlStatement);

            // Number of rows as expected?
            if (rows != ExpectedRows)
            {
                throw new ApplicationException(string.Format("EntLibDatabaseRowCountStep failed, expected {0} rows but found {1} rows", ExpectedRows, rows));
            }
            else
            {
                context.LogInfo("EntLibDatabaseRowCountStep found \"{0}\" rows", rows);
            }	// input

        }

        public void Validate(Context context)
        {
            ArgumentValidation.CheckForEmptyString(ConfigFileLocation, "Config File Location");
            ArgumentValidation.CheckForEmptyString(DatabaseConfigName, "Database Config Name");
            ArgumentValidation.CheckForEmptyString(Table, "Table");
            ArgumentValidation.CheckForEmptyString(Condition, "Condition");
            ArgumentValidation.CheckForEmptyString(ExpectedRows.ToString(), "ExpectedRows");

        }

        #endregion
    }
}
